<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="mall.demo.util.SessionKeyEnum"%>
<%@ page import="mall.demo.mybatis.pojo.ProductInCart"%>
<%@ page import="mall.demo.mybatis.pojo.User"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<%!int calcCartNumber(List<ProductInCart> productInCart) {
		if (productInCart == null || productInCart.isEmpty()) {
			return 0;
		}
		int sum = 0;
		for (ProductInCart pc : productInCart) {
			sum += pc.getAmount();
		}
		return sum;
	}%>

<%
	//获取用户登录相关信息
	Object obj = session.getAttribute(SessionKeyEnum.userObj.name());
	boolean isUserLoggedIn = false;

	if (obj != null) {
		isUserLoggedIn = true;
	}

	User userInfo = null;
	if (isUserLoggedIn) {
		userInfo = (User) obj;
	}
	List<ProductInCart> productInCart = new ArrayList<ProductInCart>();
	if (isUserLoggedIn) {
		productInCart = (List<ProductInCart>) session
				.getAttribute(SessionKeyEnum.cart.name());
		if (productInCart == null) {
			productInCart = new ArrayList<ProductInCart>();
		}
	}
%>
<div class="top">
	  	<div class="top_logo">
	  		<h2>琳琅商城</h2>
	  	</div>
		<!-- 分为登录和未登录两种情况 -->
		<div class="top_userInfo">
		<%
			if (isUserLoggedIn) {
		%>
			<b>欢迎：<%=userInfo.getLoginName()%></b>&nbsp;|
			<b><a href="cart.jsp">购物车(<span id="cartnumber"><%=calcCartNumber(productInCart)%>)</span></a></b>&nbsp;|
			<b><a href="order.jsp">我的订单</a></b>&nbsp;|
			<b><a href="logout.do">退出登录</a></b>
		<%
			} else {
		%>
			<b><a href="register.jsp">注册</a></b>
			<b><a href="login.jsp">登录</a></b>
		<%
			}
		%>
		</div>
		<br>
		<hr>
</div>
